package com.roger.myHome.component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.roger.myHome.convert.IReqMapper;
import com.roger.myHome.domain.TbWeather;
import com.roger.myHome.entity.vo.Forecast;
import com.roger.myHome.mapper.WeatherMapper;
import com.roger.myHome.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.time.LocalDate;
import java.util.*;

@Component
@Slf4j
public class InitWeather {

    @Resource
    private WeatherMapper weatherMapper;

    //private final static String HTTP_URL = "http://t.weather.itboy.net/api/weather/city/101190401";
    private final static String HTTP_URL = "https://weatherapi.market.xiaomi.com/wtr-v2/weather?cityId=101190401";

    //@Bean
    public String init() throws Exception{
        String[] s = LunarUtil.getDate(new Date());
        String ymd = s[0];
        //判断数据库有没有今日天气
        LambdaQueryWrapper<TbWeather> lambda = Wrappers.lambdaQuery();
        lambda.eq(TbWeather::getYmd, ymd);
        List<TbWeather> tbWeatherList = weatherMapper.selectList(lambda);
        if (tbWeatherList.isEmpty()) {
            StringBuffer response = HttpURLConnectionUtil.sendGet(HTTP_URL);
            Map hp = StringUtils.jsonToMap(response.toString());

            String index = hp.get("index").toString();
            List<Map> indexList = StringUtils.jsonToList(index,Map.class);

            String notice = indexList.get(indexList.size() - 1).get("details").toString();

            String data = hp.get("today").toString();
            Map hp2 = StringUtils.jsonToMap(data);

            String type = hp2.get("weatherStart") + "-" + hp2.get("weatherEnd");
            String high = "高温" + hp2.get("tempMax") + "℃";
            String low = "低温" + hp2.get("tempMin") + "℃";
            String fx = hp2.get("windDirectionStart").toString();
            String fl = hp2.get("windMin") + "-" + hp2.get("windMax") + "级";

            TbWeather tbWeather = new TbWeather();
            tbWeather.setId(SystemUtil.getId());
            tbWeather.setLunar(s[1]);
            tbWeather.setType(type);
            tbWeather.setWeek(s[2]);
            tbWeather.setHigh(high);
            tbWeather.setLow(low);
            tbWeather.setFx(fx);
            tbWeather.setFl(fl);
            tbWeather.setYmd(ymd);
            tbWeather.setNotice(notice);
            //System.err.println(tbWeather);
            weatherMapper.insert(tbWeather);
        } else {
            log.info("不用调用天气预报接口");
        }

        return "success";
    }

    @Bean
    public String init2() throws Exception{
        String[] s = LunarUtil.getDate(new Date());
        String ymd = s[0];
        //判断数据库有没有今日天气
        LambdaQueryWrapper<TbWeather> lambda = Wrappers.lambdaQuery();
        lambda.eq(TbWeather::getYmd, ymd);
        List<TbWeather> tbWeatherList = weatherMapper.selectList(lambda);
        if (tbWeatherList.isEmpty()) {
            TbWeather tbWeather = invokeWeather();
            tbWeather.setId(SystemUtil.getId());
            tbWeather.setLunar(s[1]);
            tbWeather.setWeek(s[2]);

            weatherMapper.insert(tbWeather);
        }
        return "success";
    }

    private static TbWeather invokeWeather() throws Exception{
        StringBuffer response = HttpURLConnectionUtil.sendGet("http://t.weather.itboy.net/api/weather/city/101190401");
        Map hp = StringUtils.jsonToMap(response.toString());
        String data = hp.get("data").toString();

        Map dataMap = StringUtils.jsonToMap(data);

        List<TbWeather> weatherList = StringUtils.jsonToList(dataMap.get("forecast").toString(),TbWeather.class);
        TbWeather tbWeather = weatherList.get(0);
        tbWeather.setId(SystemUtil.getId());
        //System.out.println(tbWeather);
        return tbWeather;
    }

    public static void main(String args[]) throws Exception{
        //invokeWeather();
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        System.out.println(list);
        list.remove(1);
        System.out.println(list);

        System.out.println(LocalDate.now());
    }

}
